package com.freewater.springboot.demo.ioc.lookupmethodinject.scope;

import com.freewater.springboot.demo.PackageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.context.support.SimpleThreadScope;

@Slf4j
public class Main {
    public static void main(String[] args) {
        GenericApplicationContext applicationContext = new GenericApplicationContext();
        final String xmlPath = PackageUtils.getDefaultAppContextXmlPath(Main.class);
        new XmlBeanDefinitionReader(applicationContext).loadBeanDefinitions(xmlPath);
        // 也可以通过CustomScopeConfigurer类注册自定义的Scope
        applicationContext.getBeanFactory().registerScope("thread", new SimpleThreadScope());
        applicationContext.refresh();
        log.info("使用ApplicationContext：{}", xmlPath);
        for (int i = 0; i < 2; i++) {
            new Thread(() -> {
                BeanA beanA = applicationContext.getBean(BeanA.class);
                log.info("thread: {}, beanA: {}", Thread.currentThread().threadId(), beanA);
                beanA = applicationContext.getBean(BeanA.class);
                log.info("thread: {}, beanA: {}", Thread.currentThread().threadId(), beanA);
            }).start();
        }
    }
}
